﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class ListBrand : System.Web.UI.Page
    {
        cmsBrandDO objBrand = new cmsBrandDO();
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                view();
            }
        }
        void view()
        {
            gvAt.DataSource = new cmsBrandBL().SelectAll();
            gvAt.DataBind();
            pnView.Visible = true;
            pnInsert.Visible = false;
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnView.Visible = false;
            pnInsert.Visible = true;
            Insert = true;
        }

        private void initObject()
        {
            objBrand = new cmsBrandBL().Select(objBrand);
            objBrand.Name = txtName.Text;
            objBrand.Description = txtDescription.Text;
            //objBrand.Description = txtDescription.Content;

            if (!string.IsNullOrEmpty(fuImage.FileName))
                objBrand.Logo = UploadFile(fuImage);
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtName.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                FileName = FileName.Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(" ", "-").Replace("/", "-").Replace("?", "-").Replace("\\", "-").Replace("'", "-").Replace(":", "-").Replace("\"", "-");
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/images/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        protected void gvAt_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAt.PageIndex = e.NewPageIndex;
        }

        protected void gvAt_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsBrandBL().Delete(new cmsBrandDO { BrandID = Convert.ToInt32(gvAt.DataKeys[e.RowIndex].Value) });
            
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }

        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            
            
            if (Insert)
            {
               
                new cmsBrandBL().Insert(objBrand);
            }
            else
            {
                int BrandID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
                objBrand.BrandID = BrandID;
                new cmsBrandBL().Update(objBrand);
            }
            Functions.Alert("Cập nhật thành công!");
            view();
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            view();
        }

        protected void gvAt_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvAt_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int BrandID = int.Parse(gvAt.DataKeys[gvAt.SelectedIndex].Value.ToString());
            objBrand.BrandID = BrandID;
            objBrand = new cmsBrandBL().Select(objBrand);
            txtName.Text = objBrand.Name;
            txtDescription.Text = objBrand.Description;
            //txtDescription.Content = objBrand.Description;
            if (!string.IsNullOrEmpty(objBrand.Logo))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "/images/" + objBrand.Logo;
            }
            pnInsert.Visible = true;
            pnView.Visible = false;
        }

    }
}